package com.css.fxfzaqbz.modules.assess.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.base.upload.entity.AttachmentInfoEntity;
import com.css.fxfzaqbz.base.upload.service.AttachmentInfoService;
import com.css.fxfzaqbz.modules.assess.entity.RiskAssessmentEntity;
import com.css.fxfzaqbz.modules.assess.service.RiskAssessmentService;
import com.css.fxfzaqbz.modules.assess.vo.DisasterAssVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author y1fan
 * @create 2022-09-08 18:52
 * 地震危险性评估
 */
@RestController
@RequestMapping("/fxfzaqbz/data")
@Slf4j
public class RiskAssessmentController {

    @Resource
    private RiskAssessmentService riskAssessmentService;
    @Resource
    private AttachmentInfoService attachmentInfoService;



    /*
    * 添加上传成果
    * */
    @PostMapping("/saveRiskAssessment")
    public RestResponse saveRiskAssessment(@RequestBody RiskAssessmentEntity riskAssessmentEntity) {
         RestResponse restResponse = null ;
        try{
            RiskAssessmentEntity   riskAssessmentEntity1=riskAssessmentService.saveRiskAssessment(riskAssessmentEntity);
            restResponse = RestResponse.succeed(riskAssessmentEntity1);
        }catch (Exception e){
            log.error("新增失败",e);
                restResponse = RestResponse.fail("新增失败!");
            }
            return restResponse;
    }

    /*
    * 查询地震危险性评估表 过程稿
    * */
    @GetMapping("/getEarthEntity")
    public RestResponse getEarthEntity(String id){
        RestResponse restResponse = null;
        try {
              List<RiskAssessmentEntity> allRiskAssessment = riskAssessmentService.getAllRiskAssessment(id);
            restResponse= RestResponse.succeed(allRiskAssessment);
        } catch (Exception e) {
            log.error("查看失败",e);
            restResponse= RestResponse.fail("查看失败");
        }
        return restResponse;
    }

    /*
     * 查询地震危险性评估表 最终成果稿
     * */
    @GetMapping("/getEarthEntityTwo")
    public RestResponse getEarthEntityTwo(String id){
        RestResponse restResponse = null;
        try {
            List<RiskAssessmentEntity> allRiskAssessment = riskAssessmentService.getEarthEntityTwo(id);
            restResponse= RestResponse.succeed(allRiskAssessment);
        } catch (Exception e) {
            log.error("查看失败",e);
            restResponse= RestResponse.fail("查看失败");
        }
        return restResponse;
    }


    /*
    * 删除上传成果
    * */
    @PostMapping("/deleteRiskAssessment")
    public RestResponse deleteRiskAssessment(String id){
        try {
            riskAssessmentService.deleteRiskAssessment(id);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            return RestResponse.fail("删除失败");
        }
    }

    /*
    * 修改上传成果
    * */
    @PostMapping("/updateRiskAssessment")
    public RestResponse updateRiskAssessment(@RequestBody RiskAssessmentEntity riskAssessmentEntity){
        RestResponse restResponse = null;
        try {
            RiskAssessmentEntity riskAssessmentEntity1=  riskAssessmentService.updateRiskAssessment(riskAssessmentEntity);
            restResponse=RestResponse.succeed(riskAssessmentEntity1);
        } catch (Exception e) {
            log.error("修改失败",e);
            return RestResponse.fail("修改失败");
        }
        return restResponse;
    }


    /*
    * 根据id查询上传成果
    * */
    @GetMapping("/getRiskAssessmentById")
    public RestResponse<RiskAssessmentEntity> getRiskAssessmentById(String id) {
        RestResponse restResponse = null;
        try{
            RiskAssessmentEntity riskAssessmentById = riskAssessmentService.getRiskAssessmentById(id);
            List<AttachmentInfoEntity> list=new ArrayList<>();
            Map<String,Object> map=new HashMap<>();
            if (riskAssessmentById.getSraAssContent().equals("地震活动趋势分析")){
                if (!riskAssessmentById.getSraAssFileId().equals("")){
                    String sraAssFileId = riskAssessmentById.getSraAssFileId();
                    String[] split = sraAssFileId.split(",");
                    for (String s : split) {
                        AttachmentInfoEntity attach = attachmentInfoService.getAttach(s);
                        list.add(attach);
                    }
                    map.put("list",list);
                }


           }else {
                String sraAssFileId = riskAssessmentById.getSraAssFileId();
                AttachmentInfoEntity attach = attachmentInfoService.getAttach(sraAssFileId);
                list.add(attach);
                map.put("list",list);
            }

            map.put("riskAssessmentById",riskAssessmentById);
            restResponse = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("获取数据失败!",e);
            restResponse = RestResponse.fail("获取数据失败!");
        }
        return restResponse;
    }


    /*
     * 过程稿提交到最终成果接口
     * */
    @PostMapping("/firstSubmitRiskAssessment")
    public RestResponse firstSubmitRiskAssessment(String id){
        try {
            riskAssessmentService.firstSubmitRiskAssessment(id);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error("提交失败",e);
            return RestResponse.fail("提交失败");
        }
    }

    /*
     * 是否重复接口
     * */
    @PostMapping("/riskAssessmentIfRepetition")
    public RestResponse riskAssessmentIfRepetition(@RequestBody RiskAssessmentEntity riskAssessmentEntity){

        try {
            Boolean flag=riskAssessmentService.riskAssessmentIfRepetition(riskAssessmentEntity);
            return RestResponse.succeed(flag);
        } catch (Exception e) {
            log.error("校验失败",e);
            return RestResponse.fail("校验失败");
        }
    }

    /*
     * 添加描述接口
     * */
    @PostMapping("/addRiskAssessmentDisaster")
    public RestResponse addRiskAssessmentDisaster(@RequestBody DisasterAssVO disasterAssVO){
        try {
            String describe="";
            if (disasterAssVO.getDisasterAss()!=null && disasterAssVO.getDisasterAss().size()!=0){
                describe = StringUtils.join(disasterAssVO.getDisasterAss(), "@@@");
            }

            riskAssessmentService.addRiskAssessmentDisaster(disasterAssVO.getIds(),describe);
            return RestResponse.succeed("添加描述成果");
        } catch (Exception e) {
            log.error("添加描述失败",e);
            return RestResponse.fail("添加描述失败");
        }
    }


    /*
     * 移除接口
     * */
    @PostMapping("/removeRiskAssessmentDisasterAss")
    public RestResponse removeRiskAssessmentDisasterAss(String id){
        try {
            riskAssessmentService.removeRiskAssessmentDisasterAss(id);
            return RestResponse.succeed("移除成功");
        } catch (Exception e) {
            log.error("移除失败",e);
            return RestResponse.fail("移除失败");
        }
    }

    /*
     * 最终成果提交接口
     * */
    @PostMapping("/secondSubmitRiskAssessment/{ids}")
    public RestResponse secondSubmitRiskAssessment(@PathVariable("ids") List<String> ids){
        try {
            riskAssessmentService.secondSubmitRiskAssessment(ids);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error("提交失败",e);
            return RestResponse.fail("提交成功");
        }
    }


    /*
     * 最终成果提交接口
     * */
    @PostMapping("/whatIsThisNumber")
    public RestResponse whatIsThisNumber(@RequestBody RiskAssessmentEntity riskAssessmentEntity){
        try {
            int a=riskAssessmentService.whatIsThisNumber(riskAssessmentEntity);
            return RestResponse.succeed(a);
        } catch (Exception e) {
            log.error("提交失败",e);
            return RestResponse.fail("提交失败");
        }
    }

    /*
     * 覆盖接口
     * */
    @PostMapping("/coverRiskAssessment")
    public RestResponse coverRiskAssessment(@RequestBody RiskAssessmentEntity riskAssessmentEntity){
        try {
            riskAssessmentService.coverRiskAssessment(riskAssessmentEntity);
            return RestResponse.succeed("覆盖成功");
        } catch (Exception e) {
            log.error("覆盖失败",e);
            return RestResponse.fail("覆盖失败");
        }
    }

}
